home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / diverses / leda / incl / pers_tre.h < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-15  |  5.0 KB  |  239 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  pers_tree.h
  7. +
  8. +
  9. +  Copyright (c) 1991  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16.  
  17. #ifndef PERS_TREE_H
  18. #define PERS_TREE_H
  19.  
  20. #include <LEDA/basic.h>
  21. #include <LEDA/list.h>
  22.  
  23. typedef void (*DRAW_NODE_FCT)(double,double,void*);
  24. typedef void (*DRAW_EDGE_FCT)(double,double,double,double);
  25.  
  26. /* implementation of a node of the ephemeral red-black tree of a specific
  27.  * persistent  node, where the colors of that persistent node through the
  28.  * various versions can be found
  29.  */
  30. class NODE;
  31. class C_NODE;
  32. class F_NODE;
  33. class VER_NODE;
  34.  
  35. typedef list_item version;
  36.  
  37.  
  38. class C_NODE
  39. {
  40.   friend class pers_rb_tree;
  41.  
  42.   version vers_stamp;
  43.   C_NODE* c_link[3];
  44.   int c_right;
  45.   int c_color;
  46.   int col_value;
  47.  
  48.   OPERATOR_NEW(7)
  49.   OPERATOR_DEL(7)
  50. };
  51.  
  52. /* implementation of a node of the  ephemeral red-black tree of a specific
  53.  * persistent node, where the children of that persistent node through the
  54.  * various versions can be found
  55.  */
  56.  
  57. class F_NODE
  58. {
  59.   friend class pers_rb_tree;
  60.  
  61.   version ver_stamp;
  62.   F_NODE* f_link[3];
  63.   int f_right;
  64.   int f_color;
  65.   NODE* poin_value;
  66.  
  67.   OPERATOR_NEW(8)
  68.   OPERATOR_DEL(8)
  69. };
  70.  
  71.  
  72. /* implementation of a persistent node */
  73.  
  74. class NODE
  75. {
  76.   friend class pers_rb_tree;
  77.  
  78.   void *key;
  79.   void *info;
  80.   NODE *parent;
  81.   int right;
  82.   int is_leaf;
  83.   F_NODE *link[2];
  84.   C_NODE *red;
  85.   F_NODE *copy;
  86.  
  87.   NODE* next;  // linking all used NODES
  88.  
  89.   OPERATOR_NEW(10)
  90.   OPERATOR_DEL(10)
  91. };
  92.  
  93. /* implementation of a node (or member) of the version list */
  94.  
  95. class VER_NODE
  96.   friend class pers_rb_tree;
  97.  
  98.   NODE*  acc_pointer;
  99.   int    popul;
  100.   double ser_num;
  101.  
  102.   OPERATOR_NEW(4)
  103.   OPERATOR_DEL(4)
  104. };
  105.  
  106. typedef VER_NODE* ver_node;
  107.  
  108. declare(list,ver_node);
  109.  
  110.  
  111. struct V_LIST {
  112.  
  113. list(ver_node) vl;
  114. int count;
  115. NODE* used;
  116.  
  117. };
  118.  
  119.  
  120.  
  121. class pers_rb_tree {
  122.  
  123. protected:
  124.  
  125. V_LIST* v_list;
  126.  
  127. virtual int cmp_keys(ent& x, ent& y) { return int(x)-int(y); }
  128. virtual void print_key(ent& x)       { Print(x); }
  129.  
  130. virtual void copy_key(ent& x)        { x=x; }
  131. virtual void copy_inf(ent& x)        { x=x; }
  132. virtual void clear_key(ent& x)       { x=0; }
  133. virtual void clear_inf(ent& x)       { x=0; }
  134.  
  135.  
  136. NODE* child(int leftright, NODE *p, version i)
  137. { return(acc_step(p->link[leftright], i)); }
  138.  
  139. void* get_key(NODE *p, version i)
  140. { return (acc_step(p->copy, i))->key; }
  141.  
  142. int isleaf(NODE *p) 
  143. { //return (p == p->link[0]->poin_value); 
  144.   return p->is_leaf;
  145.  }
  146.   
  147. NODE* sibling(NODE *p, version i)
  148. { return acc_step(p->parent->link[1 - p->right], i); } 
  149.  
  150.  
  151. /* define the order of versions in the version list */
  152. int vless(version x, version y)    
  153. { return  (v_list->vl[x]->ser_num < v_list->vl[y]->ser_num); }
  154.  
  155. /* find the next to a given version in the version list */ 
  156. version  iplus(version i)
  157. { return v_list->vl.succ(i); }
  158.  
  159.  
  160. NODE*   acc_step(F_NODE *, version);
  161. version new_version(version);
  162. void    m_b_root(version);
  163.  
  164. F_NODE* f_nleaf(NODE*, version);
  165. F_NODE* f_nnode(F_NODE*, F_NODE*);
  166. F_NODE* f_rbsearch(F_NODE* p, version);
  167. void    f_rbclear(F_NODE*);
  168. int     f_insert(F_NODE*, NODE*, version);
  169. void    f_single_rot(F_NODE*, int);
  170. void    f_double_rot(F_NODE*, int);
  171.  
  172. C_NODE* c_nleaf(int, version);
  173. C_NODE* c_nnode(C_NODE*, C_NODE*);
  174. C_NODE* c_rbsearch(C_NODE* p, version);
  175. void    c_rbclear(C_NODE*);
  176. int     c_insert(C_NODE*, int, version);
  177. void    c_single_rot(C_NODE*, int);
  178. void    c_double_rot(C_NODE*, int);
  179.  
  180. NODE*   single_rot(NODE* top_node, int dir, version i);
  181. NODE*   double_rot(NODE* top_node, int dir, version i);
  182. NODE*   newnode(NODE* c1, NODE* c2, version i);
  183. NODE*   newleaf(void* val, void* inf, NODE*, NODE*, version i);
  184. int     isred(NODE* p, version i);
  185. void    up_col(C_NODE* p, int newvalue, version i);
  186. void    update(F_NODE* p, NODE* newvalue, version i);
  187.  
  188. NODE*   search(void* val, version i)     { NODE* p; return search(val,p,i);}
  189. NODE*   search(void*, NODE*&, version);
  190.  
  191.  
  192. public:
  193.  
  194. // pers_rb_tree();
  195. //~pers_rb_tree();
  196.  
  197. void init_tree();
  198.  
  199. void* key(NODE* p)  { return p->key; }
  200. void* inf(NODE* p)  { return p->info; }
  201.  
  202. version del(void*, version);
  203. version insert(void*, void*, version);
  204. version change_inf(NODE*,void*,version);
  205.  
  206. NODE* lookup(void *val,version v);
  207. NODE* locate(void *val,version v);
  208. NODE* locate_pred(void *val,version v);
  209. NODE* min(version v);
  210. NODE* max(version v);
  211. NODE* succ(NODE* p, version v) { return child(1,p,v); }
  212. NODE* pred(NODE* p, version v) { return child(0,p,v); }
  213.  
  214. int   size(version v) { return v_list->vl[v]->popul; }
  215.  
  216. double ver_num(version v)  { return v_list->vl[v]->ser_num; }
  217.  
  218. void print(NODE*,version);
  219.  
  220. void del_tree();
  221.  
  222. void print(version v) 
  223. { //cout << form("%5f : ",v_list->vl[v]->ser_num);
  224.   print(v_list->vl[v]->acc_pointer,v);
  225.   newline;
  226.  }
  227.  
  228. void draw(DRAW_NODE_FCT,DRAW_EDGE_FCT, version, NODE*, double, double, double, double, double);
  229.  
  230. void draw(DRAW_NODE_FCT,DRAW_EDGE_FCT, version, double, double, double, double);
  231.  
  232. };
  233.  
  234.  
  235. #endif
  236.  
  237.